<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Device extends Controller
{
    use \App\Traits\DeviceTrait, \App\Traits\CacheTrait, \App\Traits\SessionTrait;
    public function deviceList(Request $request)
    {
        if ($request->method() == 'POST') {
            $deviceName = $request->input('name');
            $deviceDomain1 = $request->input('domain1') ? $request->input('domian1') : -1;
            $deviceDomain2 = $request->input('domain2') ? $request->input('domain2') : $deviceDomain1;
            $status = $request->input('status') ? $request->input('status') : -1;
            $pageNumber = $request->input('page', 1);
        } else {
            $deviceName = '';
            $deviceDomain2 = -1;
            $status = -1;
            $pageNumber = 1;
        }
        $otherDicList = $this->cacheGetOtherDicList();
        $domainList = $otherDicList[config('constant.domain_dic')][0];
        $pageLimit = 10;
        $postData = [
            'company_id' => $this->sessionReturnCompanyId(),
            'device_name' => $deviceName,
            'domain2' => $deviceDomain2,
            'status' => $status,
            'page_number' => $pageNumber,
            'page_limit' => $pageLimit,
        ];
        $result = $this->getDeviceList(parent::CODE_GET_COMPANY_DEVICE_LIST, $postData);
        if ($result) {
            $viewData = [
                'pageCount' => $result['pageCount'],
                'dataList' => $result['dataList'],
                'currentPage' => $pageNumber,
                'select_info' => 'device',
                'domain_list' => $domainList,
                'check_status_list' => config('constant.device_check_status'),
            ];
            return $request->method() == 'POST' ? view('admin/device/device_list_post', $viewData) : view('admin/device/device_list', $viewData);
        }
    }

    public function deviceViewInfo(Request $request)
    {
        $deviceId = $request->input('device_id');
        $postData = [
            'id' => $deviceId,
            'company_id' => $this->sessionReturnCompanyId(),
        ];
        $result = $this->getDeviceInfo(parent::CODE_GET_COMPANY_DEVICE_INFO, $postData);

        if ($result) {
            $viewData = [
                'select_info' => 'device',
                'dataInfo' => $result,
            ];
            return view('admin/device/view_device', $viewData);
        }

    }

    public function editDevice(Request $request)
    {
        $deviceId = $request->input('device_id');
        $postData = [
            'id' => $deviceId,
            'company_id' => $this->sessionReturnCompanyId(),
        ];
        $result = $this->getDeviceInfoByEdit(parent::CODE_GET_COMPANY_DEVICE_INFO_BY_EDIT, $postData);
        $otherDicList = $this->cacheGetOtherDicList();
        $deviceTypeList = $otherDicList[config('constant.device_dic')][0];
        $brandTypeList = $otherDicList[config('constant.brand_dic')][0];

        if ($result) {
            $viewData = [
                'select_info' => 'device',
                'dataInfo' => $result,
                'device_id' => $deviceId,
                'device_type_list' => $deviceTypeList,
                'brand_type_list' => $brandTypeList,
            ];
            return view('admin/device/edit_device', $viewData);
        }

    }

    public function editDeviceNumberList(Request $request)
    {
        $deviceId = $request->input('device_id');
        $postData = [
            'id' => $deviceId,
            'company_id' => $this->sessionReturnCompanyId(),
        ];
        $result = $this->getDeviceInfo(parent::CODE_GET_COMPANY_DEVICE_INFO, $postData);
        if ($result) {
            $viewData = [
                'select_info' => 'device',
                'th_list' => array_slice($result['device_number_list_th'], 1),
                'td_list' => $result['device_number_list_td'],
                'device_id' => $deviceId,
            ];
            return view('admin/device/device_number', $viewData);
        }
    }

    public function addDevice()
    {
        $otherDicList = $this->cacheGetOtherDicList();
        $deviceTypeList = $otherDicList[config('constant.device_dic')][0];
        $brandTypeList = $otherDicList[config('constant.brand_dic')][0];
        $viewData = [
            'select_info' => 'device',
            'device_type_list' => $deviceTypeList,
            'brand_type_list' => $brandTypeList,
        ];
        return view('admin/device/add_device', $viewData);
    }

}
